
        /*语法:  ES6新增的比回调函数更好的处理异步任务的对象
                1. ES6新增的一个对象
                2. 处理异步任务的
                3. 比回调函数更好

        作用: 对异步任务进行封装
                 let promise= new Promise((resovel,reject)=>{
                      //封装异步任务
                 })

                 promise.then((data)=>{
                     
                 })

        特点: 链式调用
              promise.then(()=>{

              }).then(()=>{

              }).then(()=>{

              })
              三种状态 pending  resolve  reject

         Promise.all([promise1,promise2])
         Promise.race([promise1,promise3])

         Promise.resolve(()=>{

         })
         Promise.reject(()=>{

         })


			// new Promise((resolve,reject)=>{
			//     //封装异步任务
			//     resolve()
			//     reject()
			// }).then(data=>{

			// }).catch(err=>{

			// })

			// 构造函数 + 原型  ES5面向对象
			// 类 class  ES6面向对象

			/*
          function executor(resolve,reject){
                //封装异步任务
                setTimeout(()=>{
                    if(Date.now() % 2 == 0){
                        resolve('成功')
                    }else{
                        reject('失败')
                    }
                },1000)
          }
        */
			const PENDING = 'pendding'
			const RESOLVE = 'fullful'
			const REJECT = 'reject'
			class MyPromise {
				constructor(executor) {
					let that = this
					this.value = null
					this.status = PENDING //进行中状态
					this.callBackResolves = [] // 保存成功处理的回调函数
					this.callBackRejects = [] // 保存失败结果回调函数

					function resolve(data) {
						that.status = RESOLVE //成功处理状态
						that.value = data
						if (that.status == RESOLVE) {
							that.callBackResolves.forEach(onResolve => {
								onResolve(data)
							})
						}
					}
					function reject(err) {
						that.status = REJECT //失败处理状态
						that.value = err
						if (that.status == REJECT) {
							that.callBackRejects.forEach(onReject => {
								onReject(err)
							})
						}
					}

					executor(resolve, reject)
				}
				then(onResolve) {
                    if(this.status == RESOLVE){
                        onResolve(this.value)
                        return
                    }
					this.callBackResolves.push(onResolve)
					
				}
				catch(onReject) {
                    if(this.status == REJECT){
                        onReject(this.value)
                        return
                    }
					this.callBackRejects.push(onReject)
				}
			}

			let promise = new MyPromise((resolve, reject) => {
				//封装异步任务
				setTimeout(()=>{
				    if(Date.now() % 2 == 0){
				        resolve('成功')
				    }else{
				        reject('失败')
				    }
				},1000)

				// if (Date.now() % 2 == 0) {
				// 	resolve('成功')
				// } else {
				// 	reject('失败')
				// }
			})

			promise.then(data => {
				console.log('then1', data)
			})
			promise.then(data => {
				console.log('then2', data)
			})

			promise.catch(err => {
				console.log('error ', err)
			})